import VueRouter from "vue-router"
import Vue from "vue"
import Frame from "./components/Frame"
import Login from "./components/Login"
import Index from "./components/Index"
import auth from "./utils/auth"
import ProjectList from "./components/Project/ProjectList"
import ProjectDetail from "./components/Project/ProjectDetail"
import MemberList from "./components/MemberList"
import Settings from "./components/Settings"
import Error404 from "./components/Error/Error-404"
import Error500 from "./components/Error/Error-500"

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: "/",
    component: Frame,
    children: [{
      path: "",
      component: Index,
      name: "index"
    },{
      path: "project",
      component: ProjectList,
      name: "project_list"
    }, {
      path: "project/:project_id",
      component: ProjectDetail,
      name: "project_detail"
    }, {
      path: "/users",
      component: MemberList,
      name: "member_list"
    }, {
      path: "/settings",
      component: Settings,
      name: "settings"
    }]
  }, {
    path: "/login",
    component: Login,
    name: "login"
  }, {
    path: "/error-404",
    component: Error404,
    name: "error-404"
  }, {
    path: "/error-500",
    component: Error500,
    name: "error-500"
  }]
})

router.beforeEach((to, from, next) => {
  console.log("12121212aaa")
  if (to.name != 'login' && !auth.is_authed) {
    if ((to.name != 'error-404'||to.name != 'error-500')) {
      next()
    } else {
      next({
        name: "login"
      })
    }
  } else {
    next()
  }
})

export default router